package com.ywd.materials.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywd.materials.admin.dto.MaterialsTeachVideoDto;
import com.ywd.materials.admin.dto.query.MaterialsTeachVideoQuery;
import com.ywd.materials.admin.entity.MaterialsTeachVideoEntity;
import com.ywd.materials.admin.exception.RPage;
import com.ywd.materials.admin.mapper.MaterialsTeachVideoMapper;
import com.ywd.materials.admin.vo.MaterialsTeachVideoVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:lk
 * @Date: 2025/11/13  00:16
 */
@Slf4j
@AllArgsConstructor
@Service
public class MaterialsTeachVideoServiceImpl extends ServiceImpl<MaterialsTeachVideoMapper, MaterialsTeachVideoEntity> {


    public Boolean addTeach(MaterialsTeachVideoDto dto) {
        return save(BeanUtil.copyProperties(dto, MaterialsTeachVideoEntity.class));
    }

    public Boolean updateTeach(MaterialsTeachVideoDto dto) {
        return updateById(BeanUtil.copyProperties(dto, MaterialsTeachVideoEntity.class));
    }

    public Boolean deleteTeach(Long id) {
        return removeById(id);
    }

    public RPage<List<MaterialsTeachVideoVo>> getTeachPage(MaterialsTeachVideoQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        LambdaQueryWrapper<MaterialsTeachVideoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialsTeachVideoEntity::getDeleted, 0)
                .eq(ObjectUtil.isNotEmpty(query.getType()), MaterialsTeachVideoEntity::getType, query.getType());
        List<MaterialsTeachVideoEntity> list = list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) return null;
        return RPage.ok(BeanUtil.copyToList(list, MaterialsTeachVideoVo.class), query.getPage(), query.getLimit(), page.getTotal());

    }


}
